package cn.iocoder.yudao.module.dr.controller.admin.data;

import cn.iocoder.yudao.module.dr.controller.admin.data.vo.DataPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.data.vo.DataRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.data.vo.DataSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.data.DataItemDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dr.controller.admin.data.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.data.DataDO;
import cn.iocoder.yudao.module.dr.service.data.DataService;

@Tag(name = "管理后台 - 数据共享")
@RestController
@RequestMapping("/dr/data")
@Validated
public class DataController {

    @Resource
    private DataService dataService;

    @PostMapping("/create")
    @Operation(summary = "创建数据共享")
    @PreAuthorize("@ss.hasPermission('dr:data:create')")
    public CommonResult<Long> createData(@Valid @RequestBody DataSaveReqVO createReqVO) {
        return success(dataService.createData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据共享")
    @PreAuthorize("@ss.hasPermission('dr:data:update')")
    public CommonResult<Boolean> updateData(@Valid @RequestBody DataSaveReqVO updateReqVO) {
        dataService.updateData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据共享")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:data:delete')")
    public CommonResult<Boolean> deleteData(@RequestParam("id") Long id) {
        dataService.deleteData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据共享")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:data:query')")
    public CommonResult<DataRespVO> getData(@RequestParam("id") Long id) {
        DataDO data = dataService.getData(id);
        return success(BeanUtils.toBean(data, DataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据共享分页")
    @PreAuthorize("@ss.hasPermission('dr:data:query')")
    public CommonResult<PageResult<DataRespVO>> getDataPage(@Valid DataPageReqVO pageReqVO) {
        PageResult<DataDO> pageResult = dataService.getDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据共享 Excel")
    @PreAuthorize("@ss.hasPermission('dr:data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataExcel(@Valid DataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataDO> list = dataService.getDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据共享.xls", "数据", DataRespVO.class,
                        BeanUtils.toBean(list, DataRespVO.class));
    }

    // ==================== 子表（共享数据项） ====================

    @GetMapping("/data-item/list-by-data-id")
    @Operation(summary = "获得共享数据项列表")
    @Parameter(name = "dataId", description = "共享数据主键")
    @PreAuthorize("@ss.hasPermission('dr:data:query')")
    public CommonResult<List<DataItemDO>> getDataItemListByDataId(@RequestParam("dataId") Long dataId) {
        return success(dataService.getDataItemListByDataId(dataId));
    }

}